using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace VNTViewer
{
    public partial class VNTViewer : Form
    {
        string file;
        const string VERSION = "VERSION:";
        const string BODY = "BODY;";
        const string CREATED = "DCREATED:";
        const string UPDATED = "LAST-MODIFIED:";
        const string ID = "X-IRMC-LUID:";
        const string ENCODING = "ENCODING=QUOTED-PRINTABLE:";
        const string CLASS = "CLASS:PUBLIC";
        const string END = "END:VNOTE";
        const int SCREEN_WIDTH = 750;

        public VNTViewer()
        {
            InitializeComponent();
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
            txtFilePath.Text = openFileDialog1.FileName;
            if (!string.IsNullOrEmpty(openFileDialog1.FileName))
            {
                StreamReader stream = new StreamReader(openFileDialog1.FileName, Encoding.UTF8);
                file = stream.ReadToEnd();
                string tmpVersion = file.Substring(file.IndexOf(VERSION) + VERSION.Length);
                txtVersion.Text = tmpVersion.Substring(0, tmpVersion.IndexOf(BODY)).Replace("\r\n", string.Empty);
                string tmpbody = file.Substring(file.IndexOf(ENCODING) + ENCODING.Length);
                txtBody.Text = tmpbody.Substring(0, tmpbody.IndexOf(CREATED)).Replace("=0A", "\r\n").Replace("=\r\n", string.Empty);
                string tmpCreated = file.Substring(file.IndexOf(CREATED) + CREATED.Length);
                txtCreate.Value = new DateTime(Convert.ToInt32(tmpCreated.Substring(0, 4)), Convert.ToInt32(tmpCreated.Substring(4, 2)),
                    Convert.ToInt32(tmpCreated.Substring(6, 2)), Convert.ToInt32(tmpCreated.Substring(9, 2)), Convert.ToInt32(tmpCreated.Substring(11, 2)), Convert.ToInt32(tmpCreated.Substring(13, 2)));
                string tmpupdate = file.Substring(file.IndexOf(UPDATED) + UPDATED.Length);
                txtUpdate.Value = new DateTime(Convert.ToInt32(tmpupdate.Substring(0, 4)), Convert.ToInt32(tmpupdate.Substring(4, 2)),
                    Convert.ToInt32(tmpupdate.Substring(6, 2)), Convert.ToInt32(tmpupdate.Substring(9, 2)), Convert.ToInt32(tmpupdate.Substring(11, 2)), Convert.ToInt32(tmpupdate.Substring(13, 2)));
                //txtUpdate.Text = tmpupdate.Substring(0, tmpupdate.IndexOf(CLASS)).Replace("\r\n",string.Empty);
                string tmpID = file.Substring(file.IndexOf(ID) + ID.Length);
                txtID.Text = tmpID.Substring(0, tmpID.IndexOf(END)).Replace("\r\n", string.Empty);
            }
        }

        private void openToolStripButton_Click(object sender, EventArgs e)
        {
            btnBrowse_Click(sender, e);
        }

        private void printToolStripButton_Click(object sender, EventArgs e)
        {
            printDocument1.Print();
        }

        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            Point p = new Point(50, 50);
            Font labelFont = new Font("Verdana", 16, FontStyle.Bold);
            Font textFont = new Font("Arial", 14, FontStyle.Italic);
            e.Graphics.DrawString("Filename:", labelFont, Brushes.Black, p);
            e.Graphics.DrawString(txtFilePath.Text.Substring(txtFilePath.Text.LastIndexOf('\\') + 1), textFont, Brushes.Black, p.X + 135, p.Y + 5);
            e.Graphics.DrawString("Created date:", labelFont, Brushes.Black, p.X + 325, p.Y);
            e.Graphics.DrawString(txtCreate.Value.ToString(), textFont, Brushes.Black, p.X + 520, p.Y + 5);
            p.Y += 30;
            e.Graphics.DrawLine(Pens.Black, p, new Point(p.X + e.MarginBounds.Right, p.Y));
            p.Y += 15;
            e.Graphics.DrawString("ID:", labelFont, Brushes.Black, p);
            e.Graphics.DrawString(txtID.Text, textFont, Brushes.Black, p.X + 135, p.Y + 5);
            e.Graphics.DrawString("Updated date:", labelFont, Brushes.Black, p.X + 325, p.Y);
            e.Graphics.DrawString(txtUpdate.Value.ToString(), textFont, Brushes.Black, p.X + 520, p.Y + 5);
            p.Y += 30;
            e.Graphics.DrawLine(Pens.Black, p, new Point(p.X + e.MarginBounds.Right, p.Y));
            p.Y += 15;
            e.Graphics.DrawString("Message:", labelFont, Brushes.Black, p);
            e.Graphics.DrawString("Version:", labelFont, Brushes.Black, p.X + 325, p.Y);
            e.Graphics.DrawString(txtVersion.Text, textFont, Brushes.Black, p.X + 520, p.Y + 5);
            p.Y += 30;
            int myLines = txtBody.Text.Split(new string[] { "\r\n" }, StringSplitOptions.None).Length-1;
            int tmp = Convert.ToInt32(Math.Round(textFont.GetHeight(e.Graphics) * myLines));
            Rectangle r = new Rectangle(p, new Size(e.MarginBounds.Right, tmp));
            e.Graphics.DrawRectangle(Pens.Black, r);
            textFont = new Font(textFont, FontStyle.Regular);
            e.Graphics.DrawString(txtBody.Text, textFont, Brushes.Black, r);
            textFont = new Font(textFont.FontFamily, 8, FontStyle.Italic);
            e.Graphics.DrawString("Printed:" + DateTime.Now.ToString(), textFont, Brushes.Black, r.X+595, r.Bottom+15);
        }

        private void helpToolStripButton_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtBody.Text))
                printPreviewDialog1.ShowDialog();
            else
                MessageBox.Show("No file loaded!");
        }
    }
}